package cn.zwx.infra.svc.domain.wrap;

import cn.zwx.infra.svc.domain.entity.SmsChannelEntity;
import cn.zwx.infra.svc.dto.SmsChannelSaveDTO;
import cn.zwx.infra.svc.dto.SmsChannelUpdateDTO;
import cn.zwx.infra.svc.vo.SmsChannelVO;
import org.jetbrains.annotations.NotNull;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author zhangwenxue
 */
public class SmsChannelWrap {


    /**
     * 封装短信渠道保存参数
     * @param dto  dto
     * @return SmsChannelEntity
     */
    public static SmsChannelEntity warp(SmsChannelSaveDTO dto) {
        final SmsChannelEntity smsChannel = getChannelEntity(dto);
        smsChannel.setCreator( dto.getUserId().toString());
        smsChannel.setDeleted(0);
        return smsChannel;
    }

    /**
     * 封装短信渠道保存参数
     * @param dto  dto
     * @return SmsChannelEntity
     */
    public static SmsChannelEntity warp2(SmsChannelUpdateDTO dto) {
        final SmsChannelEntity channelEntity = getChannelEntity(dto);
        channelEntity.setId(dto.getId());
        return channelEntity;
    }

    public static SmsChannelVO warp3(SmsChannelEntity smsChannel) {
        final SmsChannelVO vo = new SmsChannelVO();
        vo.setId(smsChannel.getId());
        vo.setRemark(smsChannel.getRemark());
        vo.setSignature(smsChannel.getSignature());
        vo.setStatus(smsChannel.getStatus());
        vo.setApiSecret(smsChannel.getApiSecret());
        vo.setCode(smsChannel.getChannelCode());
        vo.setApiKey(smsChannel.getApiKey());
        vo.setApiSecret(smsChannel.getApiSecret());
        vo.setCallbackUrl(smsChannel.getCallbackUrl());
        final Date date = new Date();
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
        vo.setCreateTime(localDateTime);
        return vo;
    }


    @NotNull
    private static SmsChannelEntity getChannelEntity(SmsChannelSaveDTO dto) {
        final SmsChannelEntity smsChannel = new SmsChannelEntity();
        smsChannel.setSignature(dto.getSignature());
        smsChannel.setChannelCode(dto.getCode());
        smsChannel.setStatus(dto.getStatus());
        smsChannel.setRemark(dto.getRemark());
        smsChannel.setApiKey(dto.getApiKey());
        smsChannel.setApiSecret(dto.getApiSecret());
        smsChannel.setCallbackUrl(dto.getCallbackUrl());
        smsChannel.setUpdater( dto.getUserId().toString());
        smsChannel.setUpdateTime(new Date());
        smsChannel.setCreateTime(new Date());
        smsChannel.setDeleted(0);
        return smsChannel;
    }
}
